package com.allwees.bs.module.core.dynamic.strategy.moudule;

import com.allwees.bs.core.model.merchant.entity.MerchantModel;
import com.allwees.bs.module.core.dynamic.data.DynamicInjectQuery;
import com.allwees.bs.module.core.dynamic.model.MerchantSnap;
import com.allwees.bs.module.core.dynamic.strategy.DynamicInjectStrategy;
import com.allwees.core.common.util.DynamicFieldUtil;
import lombok.Setter;
import me.zhengjie.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Setter
public class DynamicInjectRetailerStrategy extends DynamicInjectStrategy {

    private DynamicInjectQuery query;

    @Override
    public Object inject(String key, Object target) {
        MerchantModel merchant = query.findRetailerByKey(key);
        if(merchant == null){
            return target;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("company", new MerchantSnap(merchant));
        if(null != merchant.getStoreName()) {
            map.put("storeName",merchant.getStoreName());
        }

        map.put("userName", StringUtils.join(merchant.getFirstName(),merchant.getLastName()));

        if (StringUtils.isNotEmpty(merchant.getPhone())) {
            map.put("phone",merchant.getPhone());
        }

        return DynamicFieldUtil.getTarget(target, map);
    }
}
